              package com.java.diagnostics.visualizer.impl.factory;
              
              import java.io.ByteArrayInputStream;
              import java.io.ByteArrayOutputStream;
              import java.io.File;
              import java.io.FileInputStream;
              import java.io.IOException;
              import java.io.InputStream;
              import java.io.PrintStream;
              import java.net.URL;
              import java.text.MessageFormat;
              import java.util.Properties;
              import java.util.logging.Filter;
              import java.util.logging.Formatter;
              import java.util.logging.Handler;
              import java.util.logging.LogManager;
              import java.util.logging.Logger;
              import org.eclipse.core.runtime.Platform;
              import org.eclipse.osgi.service.datalocation.Location;
              
              public class DefaultLogManagerFactory extends LogManagerFactory
              {
                private static final String LOGS = "logs";
                private static final String COM_IBM_JAVA_DIAGNOSTICS_GCAndMemoryVisualizer = "com.java.diagnostics.visualizer";
                private static final String JAVA_UTIL_LOGGING_CONFIG_FILE = "java.util.logging.config.file";
                private static final String JAVA_UTIL_LOGGING_FILE_HANDLER_PATTERN = "java.util.logging.FileHandler.pattern";
                private static final String FILTER = ".filter";
                private static final String FORMATTER = ".formatter";
                private static final String DEBUG = "DEBUG";
                private static final String PERFORMANCE = "PERF";
                private static final String LOGGING_PROPERTIES = "logging.properties";
                private static final String TRACE_PROPERTIES = "trace.properties";
                private static final String PERFORMANCE_PROPERTIES = "perftrace.properties";
                private static LogManager logManager = null;
              
                public void reset()
                {
                  initialize();
                }
              
                public LogManager getLogManager()
                {
                  if (logManager == null) {
                    initialize();
                  }
                  return logManager;
                }
              
                private static boolean initialize()
                {
                  boolean successful = false;
                  logManager = null;
                  InputStream loggingPropertiesStream = null;
              
                  String configFileProperty = 
                    System.getProperty("java.util.logging.config.file");
                  String loggingResourceName;
         //          String loggingResourceName;
                  if (configFileProperty != null) {
                    loggingResourceName = configFileProperty;
                  }
                  else
                  {
                    //String loggingResourceName;
                    if (System.getProperty("DEBUG") != null) {
                      loggingResourceName = "trace.properties";
                    }
                    else
                    {
                      //String loggingResourceName;
                      if (System.getProperty("PERF") != null)
                        loggingResourceName = "perftrace.properties";
                      else {
                        loggingResourceName = "logging.properties";
                      }
                    }
              
                  }
              
                  File file = new File(loggingResourceName);
                  try {
                    if (file.exists())
                      loggingPropertiesStream = new FileInputStream(file);
                  }
                  catch (IOException localIOException1)
                  {
                    loggingPropertiesStream = null;
                  }
              
                  if (loggingPropertiesStream == null) {
                    try {
                      URL loggingResourceUrl = LogManagerFactory.class
                        .getResource(loggingResourceName);
                      loggingPropertiesStream = loggingResourceUrl.openStream();
                    }
                    catch (IOException localIOException2) {
                      loggingPropertiesStream = null;
                    }
              
                  }
              
                  if (loggingPropertiesStream != null)
                    try
                    {
                      InputStream manipulatedStream = manipulateLogFileInStream(loggingPropertiesStream);
              
                      loggingPropertiesStream.close();
              
                      successful = initialize(manipulatedStream);
              
                      if (manipulatedStream != null) {
                        manipulatedStream.close();
                      }
              
                    }
                    catch (IOException e)
                    {
                      System.out.print(e);
                    }
                  if (loggingPropertiesStream == null) {
                    System.err.println(
                      MessageFormat.format("", 
                      new Object[] { loggingResourceName }));
                  }
              
                  return successful;
                }
              
                private static InputStream manipulateLogFileInStream(InputStream loggingPropertiesStream)
                  throws IOException
                {
                  InputStream newStream = loggingPropertiesStream;
                  Properties properties = new Properties();
                  properties.load(loggingPropertiesStream);
                  String fileName = properties
                    .getProperty("java.util.logging.FileHandler.pattern");
                  if (fileName != null) {
                    File logFile = new File(fileName);
                    if (!logFile.isAbsolute()) {
                      if ((Platform.isRunning()) && (!new File(fileName).isAbsolute())) {
                        File workspace = new File(
                          Platform.getInstanceLocation().getURL().getFile());
                        File logsDir = new File(workspace, "logs");
                        File healthcenterDir = new File(logsDir, 
                          "com.java.diagnostics.visualizer");
              
                        if ((healthcenterDir.exists()) || (healthcenterDir.mkdirs()))
                          logFile = new File(healthcenterDir, fileName);
                      }
                      else
                      {
                        File homeDirectory = new File(
                          System.getProperty("user.home"));
                        logFile = new File(homeDirectory, fileName);
                      }
                    }
              
                    properties.setProperty("java.util.logging.FileHandler.pattern", 
                      logFile.getAbsolutePath());
              
                    ByteArrayOutputStream tempStream = new ByteArrayOutputStream();
                    properties.store(tempStream, "");
              
                    newStream = new ByteArrayInputStream(tempStream.toByteArray());
                  }
                  return newStream;
                }
              
                private static boolean initialize(InputStream loggingPropertiesStream)
                {
                  if (loggingPropertiesStream != null) {
                    boolean result;
                    try { logManager = LogManager.getLogManager();
                      logManager.readConfiguration(loggingPropertiesStream);
              
                      result = true;
                    }
                    catch (IOException e)
                    {
                      //boolean result;
                      System.err.println("");
              
                      e.printStackTrace();
                      result = false;
                    }
                    Logger masterLogger = Logger.getLogger("");
              
                    Handler[] handlers = masterLogger.getHandlers();
                    for (int i = 0; i < handlers.length; i++) {
                      Handler handler = handlers[i];
                      String handlerName = handler.getClass().getName();
              
                      String formatterName = logManager.getProperty(handlerName + 
                        ".formatter");
                      addFormatter(handler, formatterName);
                      String filterName = logManager
                        .getProperty(handlerName + ".filter");
                      addFilter(handler, filterName);
                    }
              
                    return result;
                  }
                  return false;
                }
              
                private static void addFormatter(Handler handler, String formatterName)
                {
                  if (formatterName != null)
                    try {
                      Formatter formatter = 
                        (Formatter)Class.forName(formatterName)
                        .newInstance();
                      handler.setFormatter(formatter);
                    } catch (Exception e) {
                      System.out.println(
                        MessageFormat.format("", 
                        new Object[] { formatterName, e.toString() }));
                    }
                }
              
                private static void addFilter(Handler handler, String filterName)
                {
                  if (filterName != null)
                    try {
                      Filter filter = 
                        (Filter)Class.forName(filterName)
                        .newInstance();
                      handler.setFilter(filter);
                    } catch (Exception e) {
                      System.out.println(
                        MessageFormat.format("", 
                        new Object[] { filterName, e.toString() }));
                    }
                }
              }

